﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace Library
{
    public partial class GetReturnBookForm : Form
    {
        LibraryEntities1 ins = new LibraryEntities1();
        public static ObjectSet<Reader> readers;
        public static ObjectSet<InstanceBook> instanceBooks;
        public GetReturnBookForm()
        {
            InitializeComponent();
            readers = ins.Reader;
            instanceBooks = ins.InstanceBook;
        }

        private void GetReturnBookForm_Load(object sender, EventArgs e)
        {
            bookView.Columns.Add("bookId", "bookId");
            bookView.Columns["bookId"].Visible = false;

            bookView.Columns.Add("ISBN", "ISBN");
            bookView.Columns["ISBN"].ReadOnly = true;
            bookView.Columns["ISBN"].Width = 100;

            bookView.Columns.Add("Name", "Название");
            bookView.Columns["Name"].Width = 200;
            bookView.Columns["Name"].ReadOnly = true;

            bookView.Columns.Add("Location", "Место");
            bookView.Columns["Location"].Width = 200;
            bookView.Columns["Location"].ReadOnly = true;



        }

        private void button1_Click(object sender, EventArgs e)
        {
            bookView.Rows.Clear();
            string readerCard = TBReaderCard.Text;
            //ins = new LibraryEntities1();
            //readers = ins.Reader;
            Reader reader = readers.Where(x => x.ReadersCard.Equals(readerCard)).FirstOrDefault();

            if (reader != null)
            {
                textBox2.Text = reader.FIO;
                if (reader.InstanceBook.Count == 0)
                {
                    MessageBox.Show("У читателя нет книг на руках");
                }
                else
                {
                    int i = 0;
                    foreach (InstanceBook instanceBook in reader.InstanceBook)
                    {
                        bookView.Rows.Add();
                        bookView.Rows[i].Cells[0].Value = instanceBook.InventoryNumber;
                        bookView.Rows[i].Cells[1].Value = instanceBook.Book.ISBN;
                        bookView.Rows[i].Cells[2].Value = instanceBook.Book.BookName;
                        bookView.Rows[i].Cells[3].Value = instanceBook.Book.Location;
                        i++;
                    }
                }
            }
            else
                MessageBox.Show("Такого читателя не существует");
        }

        private void выдатьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Reader reader = readers.Where(x => x.ReadersCard.Equals(TBReaderCard.Text)).FirstOrDefault();
            if (reader != null)
            {
                SearchForm fm = new SearchForm(reader);
                fm.ShowDialog();
                LibraryEntities1 ins1 = new LibraryEntities1();
                ObjectSet<Reader> readers1 = ins1.Reader;
                reader = readers1.Where(x => x.ReaderId == reader.ReaderId).FirstOrDefault();
                bookView.Rows.Clear();
                int i = 0;
                foreach (InstanceBook instanceBook in reader.InstanceBook)
                {
                    bookView.Rows.Add();
                    bookView.Rows[i].Cells[0].Value = instanceBook.InventoryNumber;
                    bookView.Rows[i].Cells[1].Value = instanceBook.Book.ISBN;
                    bookView.Rows[i].Cells[2].Value = instanceBook.Book.BookName;
                    bookView.Rows[i].Cells[3].Value = instanceBook.Book.Location;
                    i++;
                }
            }
            else
                MessageBox.Show("Выберите читателя");
        }

        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void принятьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (bookView.CurrentRow != null)
            {

                string elementId = bookView.CurrentRow.Cells[0].Value.ToString();
                InstanceBook element = instanceBooks.Where(x => x.InventoryNumber == elementId).FirstOrDefault();
                element.readerId = null;
                ins.SaveChanges();
                bookView.Rows.Remove(bookView.CurrentRow);

            }
            else
            {
                MessageBox.Show("Выберите книгу");
            }
        }
    }
}
